<template>
  <div class="app-container">
    <div class="card-box">
      <el-space>
        <el-select v-model="params.supplier" style="width: 300px" placeholder="请选择供应商">
          <el-option label="全部" value="all" />
          <el-option
            v-for="item in supplierAgentStore.list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <el-select v-model="params.status" style="width: 300px" placeholder="请选择状态">
          <el-option label="全部" value="all" />
          <el-option label="待支付" value="pending" />
          <el-option label="已支付" value="paid" />
          <el-option label="已发货" value="express" />
          <el-option label="已完成" value="completed" />
          <el-option label="已取消" value="canceled" />
        </el-select>
        <el-button type="primary" @click="getList">搜索</el-button>
      </el-space>
    </div>
    <div class="card-box">
      <el-table
        v-loading="supplierAgentStore.loading.transactionList"
        :data="supplierAgentStore.transactionList"
        row-id="id"
      >
        <el-table-column label="ID" prop="id" />
        <el-table-column label="订单商品名称" prop="supplier.name" />
        <el-table-column label="订单金额" prop="payment_amount" />
        <el-table-column label="订单日期" prop="created_at" />
        <el-table-column label="订单状态">
          <template #default="{ row }">
            <el-tag v-if="row.status == 'pending'" type="info">待支付</el-tag>
            <el-tag v-if="row.status == 'paid'" type="primary">已支付</el-tag>
            <el-tag v-if="row.status == 'express'" type="warning">已发货</el-tag>
            <el-tag v-if="row.status == 'completed'" type="success">已完成</el-tag>
            <el-tag v-if="row.status == 'canceled'" type="danger">已取消</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.size"
        style="margin-top: 10px"
        background
        :total="supplierAgentStore.totalCount"
        @change="getList"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { type SupplierAgentTransactionListQuery } from "@/api/supplier/agent";
import { useSupplierAgentStoreHook } from "@/store/modules/supplier/agent";

const supplierAgentStore = useSupplierAgentStoreHook();

const route = useRoute();

const params = reactive<SupplierAgentTransactionListQuery>({
  supplier: Number(route.query.supplier_id) || "all",
  status: "all",
  page: 1,
  size: 10,
});

function getList() {
  const temParams: SupplierAgentTransactionListQuery = JSON.parse(JSON.stringify(params));
  if (temParams.supplier == "all") {
    delete temParams.supplier;
  }
  if (temParams.status == "all") {
    delete temParams.status;
  }
  supplierAgentStore.fetchTransactionList(temParams);
}

onMounted(async () => {
  await supplierAgentStore.fetchList({
    page: 1,
    size: 1000,
  });
  getList();
});
</script>

<style scoped lang="scss"></style>
